package com.CGFrame.struts.bean;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Date;
import java.util.Map;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;


public class GenerateReportBean {
//	@param jasperFile example:"/***/*.jrxml" JasperDesign file;parameters 参数列表;beans 作为数据源的javabean数组
	public static String generatePDFFileByBeanArray(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
		if(jrxmlFile!=null){
			//生成目标文件的文件名
			String destFilePath=new String(jrxmlFile.substring(0, jrxmlFile.lastIndexOf(File.separator)));
			String destFileName=File.separator+String.valueOf((new Date().getTime()))+".pdf";
			//报表输出成PDF
			JasperExportManager.exportReportToPdfFile(generateJasperPrint(jrxmlFile,parameters,beans),destFilePath+destFileName);
			return destFileName;
		}
		return null;
	}
	
//	@param jasperFile example:"/***/*.jrxml" JasperDesign file;parameters 参数列表;beans 作为数据源的javabean数组
	public static String generateExcelFileByBeanArray(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
		if(jrxmlFile!=null){
//			生成目标文件的文件名
			String destFilePath=new String(jrxmlFile.substring(0, jrxmlFile.lastIndexOf(File.separator)));
			String destFileName=File.separator+String.valueOf((new Date().getTime()))+".xls";
			
			JRXlsExporter exporter=new JRXlsExporter();
			exporter.setParameters(parameters);
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, generateJasperPrint(jrxmlFile,parameters,beans));
			exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFilePath+destFileName);
			
			exporter.exportReport();
			
			return destFileName;
		}
		return null;
	}
	
//	@param jasperFile example:"/***/*.jrxml" JasperDesign file;parameters 参数列表;beans 作为数据源的javabean数组
	public static String generateHTMLFileByBeanArray(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
		if(jrxmlFile!=null){
			//生成目标文件的文件名
			String destFilePath=new String(jrxmlFile.substring(0, jrxmlFile.lastIndexOf(File.separator)));
			String destFileName=File.separator+String.valueOf((new Date().getTime()))+".html";
			//报表输出成PDF
			JasperExportManager.exportReportToHtmlFile(generateJasperPrint(jrxmlFile,parameters,beans),destFilePath+destFileName);
			return destFileName;
		}
		return null;
	}
	
	public static byte[] generatePDFByBeanArray(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
		if(jrxmlFile!=null){
			//报表输出成PDF
			byte[] bytes=JasperExportManager.exportReportToPdf(generateJasperPrint(jrxmlFile,parameters,beans));
			return bytes;
		}
		return null;
	}
	
	public static byte[] generateExcelByBeanArray(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
		if(jrxmlFile!=null){		
			JRXlsExporter exporter=new JRXlsExporter();
			exporter.setParameters(parameters);
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, generateJasperPrint(jrxmlFile,parameters,beans));
			
			ByteArrayOutputStream oStream = new ByteArrayOutputStream();
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			
			exporter.exportReport();
			byte[] bytes=oStream.toByteArray();
			return bytes;
		}
		return null;
	}
	
	public static byte[] generateHTMLByBeanArray(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
		if(jrxmlFile!=null){		
			JRHtmlExporter exporter=new JRHtmlExporter();
			exporter.setParameters(parameters);
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, generateJasperPrint(jrxmlFile,parameters,beans));
			
			ByteArrayOutputStream oStream = new ByteArrayOutputStream();
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			
			exporter.exportReport();
			byte[] bytes=oStream.toByteArray();
			return bytes;
		}
		return null;
	}
	
//	@param jasperFile example:"/***/*.jrxml" JasperDesign file;parameters 参数列表;beans 作为数据源的javabean数组
	private static JasperPrint generateJasperPrint(String jrxmlFile,Map parameters,Object[] beans) throws Exception{
			//把*.jrxml编译成*.jasper
			JasperReport jr=JasperCompileManager.compileReport(jrxmlFile);
			
			//创建一个javabean数组的数据源
			JRBeanArrayDataSource jrDateSource=new JRBeanArrayDataSource(beans);
			
			//用数据源填充报表
			JasperPrint jp=JasperFillManager.fillReport(jr, parameters, jrDateSource);
			
			return jp;
	}
}
